// index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httphost: app.data.httphost,
    httpserver: app.data.httpserver,
    loginSession:"",
  },

  order_list: function (event) {
     // 调用API从本地缓存中获取数据
     var session_id = wx.getStorageSync('session_id');
     var header = wx.getStorageSync('header');
     if (session_id == '') {
       wx.showToast({
         title: '您还未登录，请先登录！',
         icon: 'none',
         duration: 2000
       })
       return;
     }
    var status = event.currentTarget.dataset.status;
    wx.navigateTo({
      url: '../order/list/list?status=' + status,
    })
  },
   collect_list: function () {
      // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    if (session_id == '') {
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.navigateTo({
      url: '../collect/list',
    })
  },
   address_list: function () {
      // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    if (session_id == '') {
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
     wx.navigateTo({
       url: '../address/list/list',
     })
   },
   coupon_list: function () {
      // 调用API从本地缓存中获取数据
      var session_id = wx.getStorageSync('session_id');
      if (session_id == '') {
        wx.showToast({
          title: '您还未登录，请先登录！',
          icon: 'none',
          duration: 2000
        })
        return;
      }
     wx.navigateTo({
       url: '../coupon/user/list/list',
     })
   },
   user_index: function () {
      // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    if (session_id == '') {
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
     wx.navigateTo({
       url: '../user/index/index',
     })
   },
   user_score: function () {
     // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    if (session_id == '') {
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    wx.redirectTo({
      url: '../score/index',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this ;
    // 调用API从本地缓存中获取数据
    var session_id = wx.getStorageSync('session_id');
    if (session_id == '') {
      that.setData({
        loginSession: '0'
      })
      wx.showToast({
        title: '您还未登录，请先登录！',
        icon: 'none',
        duration: 2000
      })
      return;
    }else{
      that.setData({
        loginSession: '1'
      })
    }

    wx.request({
      url: that.data.httpserver+'center/index',
      header: wx.getStorageSync('header'),
      success: function (request){
        that.setData({
          count: request.data.count,
          shopUser: request.data.shopUser,
          cart_count: request.data.cart_count,
        })
      }
    })
  },
   /**
   * Lgd 获取用户信息
   *  
   */
  bindGetUserInfo(res){
    console.log("----登录----"+res)
    if(res.detail.errMsg =="getUserInfo:ok"){
      
//function (options) {
console.log("-----------------欢迎登录")
var that = this;
wx.login({
  success: function (r) {
    var code = r.code;//登录凭证
    console.log(code);
    if (code) {
      //2、调用获取用户信息接口
      var session_id = wx.getStorageSync('session_id');//本地取存储的sessionID  
      console.log("^^^^^^^^^^^^^^^^"+session_id+"-----"+app.data.httpserver + 'app/weixinlogin')
      //3.请求自己的服务器，解密用户信息 获取unionId等加密信息
          wx.request({
            url: app.data.httpserver + 'app/weixinlogin',//自己的服务接口地址
            data: { encryptedData: res.detail.encryptedData, iv: res.detail.iv, code: code },
            success: function (request) {
              //4.解密成功后 获取自己服务器返回的结果
              console.log(request)
              if (request.data.result == 1) {
                var userInfo = request.data.shopUser
                var session_id = request.data.session_id
                var header = { 'content-type': 'application/x-www-form-urlencoded', 'Cookie': 'session_id=' + session_id }
                var header_upload = { 'content-type': 'multipart/form-data', 'Cookie': 'session_id=' + session_id }
                wx.setStorageSync('header', header)
                wx.setStorageSync('header_upload', header_upload)
                wx.setStorageSync('session_id', session_id)
                wx.setStorageSync("userInfo", userInfo);
                
                wx.redirectTo({
                  url: '/pages/center/index'
                })
              } else {
                console.log('解密失败')
              }
            },
            fail: function () {
              console.log('系统错误')
            }
          })
        

    } else {
      console.log('获取用户登录态失败！' + r.errMsg)
      return;
    }
  },
  fail: function () {
    console.log('登陆失败')
    return;
  }
}) 
    }else{
      wx.showModal({
        title: '提示',
        content: '您点击了拒绝授权，将无法正常使用商城功能。',
        showCancel: false,
        success: function (res) {
          
          return;
        }
      })
    }
     
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})